package hu.myai.indicators.trendline;

import hu.myai.api.indicators.AbstractIndicator;
import hu.myai.model.IndicatorVO;
import hu.myai.model.TrendLineVO;
import hu.myai.util.pool.TrendLinePool;

import java.util.List;

/**
 * Indicator increase a counter if resistance is touched and decrease the
 * counter if suport is crossed.
 * */
public class LineTouchCounterIndicator extends AbstractIndicator {

	public LineTouchCounterIndicator(Double parameter) {
		super(parameter);
	}

	private int counter;
	private int maxResTouch;
	private int maxSupTouch;

	@Override
	protected IndicatorVO indicatorCalculation() {

		List<TrendLineVO> resistanceList = TrendLinePool.getResistanceInstance().getLineList(tickSourceID);
		List<TrendLineVO> supportList = TrendLinePool.getSupportInstance().getLineList(tickSourceID);

		TrendLineVO supLine = null;
		for (TrendLineVO line : supportList) {
			double yNow = line.getY(lastTick.getTimestamp());
			// tick is below the support
			if (yNow > lastTick.getHigh() || yNow > lastTick.getClose()) {
				line.increaseTouch();

				System.out.println("sup: " + line.getTouch());
				counter--;
				supLine = line;
				break;
			}
		}

		TrendLineVO resLine = null;
		for (TrendLineVO line : resistanceList) {
			double yNow = line.getY(lastTick.getTimestamp());

			// tick is above the resistance
			if (yNow < lastTick.getLow() || yNow < lastTick.getClose()) {
				line.increaseTouch();
				counter++;
				System.out.println("res: " + line.getTouch());
				resLine = line;
				break;
			}
		}


		if (supLine != null && supLine.getTouch() == 1) {
			IndicatorVO indicator = new IndicatorVO();
			indicator.setChanged(true);
			indicator.setDate(lastTick.getTime());
			indicator.setValue(supLine.getTangens() < 0 ? 1.365 : 1.367);
//			indicator.setValue(0.0001 * counter + 1.41);
			return indicator;
		} 
//		else
//			indicator.setValue(1.41);

		if (resLine != null && resLine.getTouch() == 1) {
			IndicatorVO indicator = new IndicatorVO();
			indicator.setChanged(true);
			indicator.setDate(lastTick.getTime());
			indicator.setValue(resLine.getTangens() > 0 ? 1.371 : 1.369);
//			indicator.setValue(0.0001 * counter + 1.41);
			return indicator;
		} 

		return null;
	}

	@Override
	public void init() {
		register(new SupportLevelIndicator(parameter));
		register(new ResistanceLevelIndicator(parameter));
	}

}
